const Mode = require('frontmatter-markdown-loader/mode')
const markdownIt = require('markdown-it')
const markdownItHlJs = require('markdown-it-highlightjs')
const markdownItLA = require('markdown-it-link-attributes')
const webpack = require('webpack')
const CompressionPlugin = require('compression-webpack-plugin')
const zlib = require('zlib')

module.exports = {
  publicPath: '/',
  devServer: {

    port: 8080,
    open: false,
    headers: {
      'Access-Control-Allow-Origin': '*'
    },
    overlay: {

      warnings: false,
      errors: true
    },
    proxy: {
      // vue2.x版本为 proxyTable，vue3.x为proxy。
      // detail: https://cli.vuejs.org/config/#devserver-proxy
      '^/api': {
        // target: 'http://124.221.145.222:1001', // 开发后端端口
        target: 'http://localhost:1002', // 开发后端端口
        changeOrigin: true,
        secure: false,
        pathRewrite: {
          // 路径重写
          '^/api': '/'
        }
      }
    }
  },
  configureWebpack: {
    plugins: [
      new webpack.IgnorePlugin({
        resourceRegExp: /^\.\/locale$/,
        contextRegExp: /moment$/
      }),
      // 下面两项配置才是 compression-webpack-plugin 压缩配置
      // 压缩成 .gz 文件
      new CompressionPlugin({
        filename: '[path][base].gz',
        algorithm: 'gzip',
        test: /\.js$|\.css$|\.html$/,
        threshold: 10240,
        minRatio: 0.8
      }),
      // 压缩成 .br 文件，如果 zlib 报错无法解决，可以注释这段使用代码，一般本地没问题，需要注意线上服务器会可能发生找不到 zlib 的情况。
      new CompressionPlugin({
        filename: '[path][base].br',
        algorithm: 'brotliCompress',
        test: /\.(js|css|html|svg)$/,
        compressionOptions: {
          params: {
            [zlib.constants.BROTLI_PARAM_QUALITY]: 11
          }
        },
        threshold: 10240,
        minRatio: 0.8
      })
    ],
    module: {
      rules: [
        {
          test: /\.md$/,
          loader: 'frontmatter-markdown-loader',
          options: {
            mode: [Mode.VUE_COMPONENT],
            markdownIt: markdownIt({ html: true })
              .use(markdownItHlJs, { inline: true })
              .use(markdownItLA, {
                attrs: {
                  target: '_blank'
                }
              })
          }
        }
      ]
    }
  }
}
